//
//  MSwipesViewController.m
//  iPhoneDev01
//
//  Created by smu on 11-7-13.
//  Copyright 2011年 duk. All rights reserved.
//

#import "MSwipesViewController.h"


@implementation MSwipesViewController
@synthesize label;
@synthesize gestureStartPoint;
- (void)eraseText
{
    label.text = @"";
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [label release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    self.label = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma  mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    SwipeType swipeType = kNoSwipe;
    for (UITouch *touch in touches) {
        CGPoint currentPosition = [touch locationInView:self.view];
        CGFloat deltaX = fabsf(currentPosition.x - gestureStartPoint.x);
        CGFloat deltaY = fabsf(currentPosition.y - gestureStartPoint.y);
        
        if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
            swipeType = kHorizontalSwipe;
        } else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
            swipeType = kVerticalSwipe;
        }
    }
    BOOL allFingersFarEnoughAway = YES;
    if (swipeType != kNoSwipe) {
        for (UITouch *touch in touches) {
            CGPoint currentPosition = [touch locationInView:self.view];
            
            CGFloat distance;
            if (swipeType == kHorizontalSwipe) {
                distance = fabsf(currentPosition.x - gestureStartPoint.x);
            } else {
                distance = fabsf(currentPosition.y - gestureStartPoint.y);
            }
            
            if (distance < kMinimumGestureLength) {
                allFingersFarEnoughAway = NO;
            }
        }
    }
    if (allFingersFarEnoughAway && swipeType != kNoSwipe) {
        NSString *swipeCountString = nil;
        if ([touches count] == 2) {
            swipeCountString = @"Double";
        } else if ([touches count] == 3) {
            swipeCountString = @"Triple";
        } else if ([touches count] == 4) {
            swipeCountString = @"Quadruple";
        } else if([touches count] == 5) {
          swipeCountString = @"Quintuple";  
        } else {
            swipeCountString = @"";
        }
        NSString *swipeTypeString = (swipeType = kHorizontalSwipe) ?
        @"Horizontal" : @"Vertical";
        
        NSString *message = [[NSString alloc] initWithFormat:
                             @"%@%@ Swipe Detected.",swipeCountString,swipeTypeString];
        label.text = message;
        [message release];
        [self performSelector:@selector(eraseText)
                        withObject:nil
                   afterDelay:2];
    }
}
@end
